package EXEX;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import network.MultiChatServerThread;
import network.SocketData;

import javax.swing.JTextField;
import javax.swing.ImageIcon;

import java.awt.Font;
import java.awt.Color;

public class rspserver extends JFrame implements Runnable {

	ServerSocket server = null;
	Socket client = null;
	rspserverThread rst = null;
	rspClientThread rct = null;
	int port = 4444;
	boolean tflag = false;
	HashMap<String, rspserverThread> myClients = new HashMap<String, rspserverThread>();
	
	private JPanel contentPane;
	private JButton serverstartbtn;
	private JButton serverdownbtn;
	JButton gamestartbtn;
	JButton sbtn;
	JButton rbtn;
	JButton pbtn;
	private JLabel serverlbl;
	JLabel userlbl;
	JLabel myname;
	JLabel username;
	JLabel userstatus;
	JLabel time1;
	JLabel serverresult;
	JLabel result1;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					rspserver frame = new rspserver();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public rspserver() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(0, 0, 527, 567);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getServerstartbtn());
		contentPane.add(getServerdownbtn());
		contentPane.add(getGamestartbtn());
		contentPane.add(getSbtn());
		contentPane.add(getRbtn());
		contentPane.add(getPbtn());
		contentPane.add(getServerlbl());
		contentPane.add(getUserlbl());
		contentPane.add(getMyname());
		contentPane.add(getUsername());
		contentPane.add(getUserstatus());
		contentPane.add(getTime1());
		contentPane.add(getServerresult());
		contentPane.add(getResult1());
		
		JLabel VS = new JLabel("VS");
		VS.setFont(new Font("굴림", Font.BOLD, 70));
		VS.setBounds(200, 204, 97, 89);
		contentPane.add(VS);
		try {
			InetAddress ia = InetAddress.getLocalHost();
			setTitle("ip : " + ia.getHostAddress() + "   port : " + port);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private JButton getServerstartbtn() {
		if (serverstartbtn == null) {
			serverstartbtn = new JButton("게임서버 시작");
			serverstartbtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					serverStart();
					serverstartbtn.setEnabled(false);
					serverdownbtn.setEnabled(true);
					myname.setText("Server");
					username.setText("접속 대기중...");
					serverlbl.setIcon(new ImageIcon("D:\\server.jpg"));
				}
			});
			serverstartbtn.setBounds(237, 10, 120, 30);
		}
		return serverstartbtn;
	}

	private JButton getServerdownbtn() {
		if (serverdownbtn == null) {
			serverdownbtn = new JButton("게임서버 종료");
			serverdownbtn.setEnabled(false);
			serverdownbtn.setBounds(368, 10, 120, 30);
		}
		return serverdownbtn;
	}

	private JButton getGamestartbtn() {
		if (gamestartbtn == null) {
			gamestartbtn = new JButton("게임시작");
			gamestartbtn.setEnabled(false);
			gamestartbtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					gameStart();
					gamestartbtn.setEnabled(false);
					rbtn.setEnabled(true);
					sbtn.setEnabled(true);
					pbtn.setEnabled(true);
				}
			});
			gamestartbtn.setBounds(12, 10, 97, 30);
		}
		return gamestartbtn;
	}

	private JButton getSbtn() {
		if (sbtn == null) {
			sbtn = new JButton("");
			sbtn.setIcon(new ImageIcon("D:\\크기변환_가위.jpg"));
			sbtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try{
						rst.serversel = 1;
						rbtn.setEnabled(false);
						sbtn.setEnabled(false);
						pbtn.setEnabled(false);
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			});
			sbtn.setEnabled(false);
			sbtn.setBounds(12, 423, 78, 100);
		}
		return sbtn;
	}

	private JButton getRbtn() {
		if (rbtn == null) {
			rbtn = new JButton("");
			rbtn.setIcon(new ImageIcon("D:\\크기변환_바위.jpg"));
			rbtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try{
						rst.serversel = 2;
						rbtn.setEnabled(false);
						sbtn.setEnabled(false);
						pbtn.setEnabled(false);
					}catch(Exception ex){
						ex.printStackTrace();
					}
				
				}
			});
			rbtn.setEnabled(false);
			rbtn.setBounds(197, 423, 97, 100);
		}
		return rbtn;
	}

	private JButton getPbtn() {
		if (pbtn == null) {
			pbtn = new JButton("");
			pbtn.setIcon(new ImageIcon("D:\\크기변환_보.jpg"));
			pbtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try{
						rst.serversel = 3;
						rbtn.setEnabled(false);
						sbtn.setEnabled(false);
						pbtn.setEnabled(false);					
					}catch(Exception ex){
						ex.printStackTrace();
					}
				
				}
			});
			pbtn.setEnabled(false);
			pbtn.setBounds(384, 423, 104, 100);
		}
		return pbtn;
	}

	private JLabel getServerlbl() {
		if (serverlbl == null) {
			serverlbl = new JLabel("");
			serverlbl.setHorizontalAlignment(SwingConstants.CENTER);
			serverlbl.setBounds(22, 178, 150, 150);
		}
		return serverlbl;
	}

	private JLabel getUserlbl() {
		if (userlbl == null) {
			userlbl = new JLabel("");
			userlbl.setHorizontalAlignment(SwingConstants.CENTER);
			userlbl.setBounds(338, 178, 150, 150);
		}
		return userlbl;
	}

	
	
	public void serverStart(){
		try{
			Thread t = new Thread(this);
			t.start();
			JOptionPane.showMessageDialog(null, "서버 오픈");
		}catch(Exception ex){
			ex.printStackTrace();
		}	
	}
	
	public void serverDown(){
//		tflag = false;
//		
//		//server.accept() 의 블로킹 모드 해제
//		Socket s = new Socket("127.0.0.1", 4444);
//		//접속된 유저들에게 서버가 중지되었음을 전달하기 위해
//		SocketData sd = new SocketData("ServerDown", "server", " 서버가 종료되었습니다.", null);
//		if(!clients.isEmpty()) mcst.sendMsg(sd);
//		ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
//		oos.writeObject(sd);
//		
//		clients.clear();
//		user.setListData(new Vector<String>());
//		
//		if(server != null) {server.close();}		
	}
	
	public void gameStart(){
		try{
			rspData rd = new rspData("gameStart", "Server", null);
			rst.send.writeObject(rd);
			rst.gamestr = 1;
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	
	@Override
	public void run() {
		try{
			server = new ServerSocket(port);
			tflag = true;
			while(tflag){
				client = server.accept();
				
				rst = new rspserverThread(this, client);
				rst.start();
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}	
	}
	private JLabel getMyname() {
		if (myname == null) {
			myname = new JLabel("");
			myname.setFont(new Font("굴림", Font.BOLD, 20));
			myname.setBounds(60, 145, 67, 23);
		}
		return myname;
	}
	private JLabel getUsername() {
		if (username == null) {
			username = new JLabel("");
			username.setFont(new Font("굴림", Font.BOLD, 20));
			username.setBounds(349, 145, 150, 23);
		}
		return username;
	}
	private JLabel getUserstatus() {
		if (userstatus == null) {
			userstatus = new JLabel("");
			userstatus.setFont(new Font("굴림", Font.BOLD, 20));
			userstatus.setForeground(new Color(0, 128, 0));
			userstatus.setBounds(356, 353, 110, 30);
		}
		return userstatus;
	}
	private JLabel getTime1() {
		if (time1 == null) {
			time1 = new JLabel("");
			time1.setHorizontalAlignment(SwingConstants.CENTER);
			time1.setForeground(Color.RED);
			time1.setFont(new Font("굴림", Font.BOLD, 20));
			time1.setBounds(130, 159, 229, 20);
		}
		return time1;
	}
	private JLabel getServerresult() {
		if (serverresult == null) {
			serverresult = new JLabel("");
			serverresult.setFont(new Font("굴림", Font.BOLD, 30));
			serverresult.setBounds(264, 65, 192, 70);
		}
		return serverresult;
	}
	private JLabel getResult1() {
		if (result1 == null) {
			result1 = new JLabel("");
			result1.setFont(new Font("굴림", Font.BOLD, 20));
			result1.setBounds(12, 50, 282, 20);
		}
		return result1;
	}
}
